package mop.gui.task.focuswork;

import java.text.SimpleDateFormat;
import java.util.LinkedList;
import java.util.List;
import javax.swing.JButton;
import javax.swing.JPanel;
import mop.controller.FocusWorkController;
import mop.controller.ModifyTaskDetailsController;
import mop.controller.UpdateTaskStatusController;
import mop.gui.GUI;
import mop.gui.task.TaskDetailsAction;
import mop.gui.task.TaskStatusAction;
import mop.model.task.TaskInterface;

/**
 * List of focus work tasks.
 *
 * @author  MOP Team 7
 */
public class FocusWorkPanel extends javax.swing.JPanel
{
    /**
     * Default constructor.
     */
    public FocusWorkPanel()
    {
        initComponents();
    }

    /**
     * Constructor.
     *
     * @param   gui
     */
    public FocusWorkPanel(GUI gui)
    {
        initComponents();
        this.setGui(gui);
    }

    /***************************************************************************
     *                          GUI reference
     ***************************************************************************/

    private GUI gui;

    /**
     * Sets the GUI.
     *
     * @param   gui
     * @post    gui is set.
     */
    private void setGui(GUI gui)
    {
        this.gui = gui;
    }

    /**
     * Returns the GUI reference.
     *
     * @return  this.gui
     */
    public GUI getGui()
    {
        return this.gui;
    }

    /***************************************************************************
     *                             controllers
     ***************************************************************************/

    private FocusWorkController focusWorkController;
    private ModifyTaskDetailsController modifyTaskDetailsController;
    private UpdateTaskStatusController updateTaskStatusController;

    /**
     * Adds a controller.
     *
     * @param   focusWorkController
     */
    public void addController(FocusWorkController focusWorkController) {
        // Controller
        this.focusWorkController = focusWorkController;

        // Add different types of focus work.
        this.focusWorkPanels = new LinkedList<FocusWorkInterface>();
        this.focusWorkPanels.add(new DeadlineBasedFocusPanel(this, gui, focusWorkController));
        this.focusWorkPanels.add(new DurationBasedFocusPanel(this, gui, focusWorkController));
        this.focusWorkPanels.add(new TaskTypeBasedFocusPanel(this, gui, focusWorkController));

        // Add all to pane.
        for (FocusWorkInterface p : this.focusWorkPanels)
            this.focusWorkPane.add(p.getTitle(), (JPanel)p);
    }

    /**
     * Adds a controller.
     *
     * @param   modifyTaskDetailsController
     */
    public void addController(ModifyTaskDetailsController modifyTaskDetailsController) {
        this.modifyTaskDetailsController = modifyTaskDetailsController;
    }

    /**
     * Adds a controller.
     *
     * @param   updateTaskStatusController
     */
    public void addController(UpdateTaskStatusController updateTaskStatusController) {
        this.updateTaskStatusController = updateTaskStatusController;
    }

    /***************************************************************************
     *                             Focus Work
     ***************************************************************************/

    private List<FocusWorkInterface> focusWorkPanels;

    enum FocusType {
        DEADLINE,
        DURATION,
        TASKTYPE
    }

    /**
     * Commands all focus work panels to update.
     */
    public void update()
    {
        for (FocusWorkInterface f : this.focusWorkPanels)
            f.focus();
    }

    /**
     * Generates the rows for a "focus work" table.
     *
     * @param   focusTasks
     *          List of tasks.
     * @return  The rows for the table.
     */
    public Object[][] generateFocusWorkRows(GUI gui, LinkedList<TaskInterface> focusTasks, FocusType type)
    {
        Object[][] rows = new Object[focusTasks.size()][5];

        for ( int i = 0 ; i < focusTasks.size() ; i++ )
        {
            rows[i][0] = focusTasks.get(i).getId();

            rows[i][1] = focusTasks.get(i).getStatus().getName();

            switch (type) {
                case DEADLINE:
                    SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-yyyy HH:mm");
                    rows[i][2] = formatter.format(focusTasks.get(i).getSchedule().getDeadline());
                    break;
                case DURATION:
                    rows[i][2] = Long.toString(focusTasks.get(i).getSchedule().getDuration().getTime() /  3600000); // in hours
                    break;
                case TASKTYPE:
                    rows[i][2] = focusTasks.get(i).getTaskType().getId();
                    break;
                default:
                    rows[i][2] = "???";
            }

            rows[i][3] = new JButton("Details");
            if (modifyTaskDetailsController != null)
                ((JButton)rows[i][3]).addMouseListener(new TaskDetailsAction(gui, modifyTaskDetailsController, focusTasks.get(i)));
            else
                ((JButton)rows[i][3]).setEnabled(false);

            rows[i][4] = new JButton("Change Status");
            if (updateTaskStatusController != null)
                ((JButton)rows[i][4]).addMouseListener(new TaskStatusAction(gui, updateTaskStatusController, focusTasks.get(i)));
            else
                ((JButton)rows[i][4]).setEnabled(false);
        }

        return rows;
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        focusWorkPane = new javax.swing.JTabbedPane();
        lblFocusWork = new javax.swing.JLabel();
        btnUpdateFocus = new javax.swing.JButton();

        setPreferredSize(new java.awt.Dimension(517, 569));

        lblFocusWork.setFont(new java.awt.Font("Tahoma", 1, 18));
        lblFocusWork.setText("Focus Work");

        btnUpdateFocus.setText("Update Focus");
        btnUpdateFocus.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnUpdateFocusActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 517, Short.MAX_VALUE)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addContainerGap()
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(focusWorkPane, javax.swing.GroupLayout.DEFAULT_SIZE, 497, Short.MAX_VALUE)
                        .addGroup(layout.createSequentialGroup()
                            .addComponent(lblFocusWork)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 292, Short.MAX_VALUE)
                            .addComponent(btnUpdateFocus)))
                    .addContainerGap()))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 569, Short.MAX_VALUE)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addGap(11, 11, 11)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(lblFocusWork)
                        .addComponent(btnUpdateFocus))
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addComponent(focusWorkPane, javax.swing.GroupLayout.DEFAULT_SIZE, 515, Short.MAX_VALUE)
                    .addContainerGap()))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void btnUpdateFocusActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnUpdateFocusActionPerformed
        this.update();
}//GEN-LAST:event_btnUpdateFocusActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnUpdateFocus;
    private javax.swing.JTabbedPane focusWorkPane;
    private javax.swing.JLabel lblFocusWork;
    // End of variables declaration//GEN-END:variables

}
